package LINE.comm.resolver;

import java.io.File;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONSerializer;

//import org.apache.commons.fileupload.FileUpload;
//import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.log4j.Logger;
//import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebArgumentResolver;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

//import com.with.biz.cmm.service.CommService;
//import com.with.biz.cmm.vo.SessionVO;
//import com.with.biz.zz.service.zz00Service;
//import com.with.comm.util.DateFormat;
//import com.with.comm.util.SessionUtil;
import LINE.comm.util.StringUtil;

//import egovframework.com.utl.fcc.service.EgovFormBasedFileUtil;


/**
 * 
 * @since 2012.03.26
 * @author devk
 *
 *
 */
public class CommandMapArgumentResolver implements WebArgumentResolver {

	Logger logg = Logger.getLogger(CommandMapArgumentResolver.class);

	
	private JSONArray getJsonObject(String jsondata){
		JSONArray jsonArray = JSONArray.fromObject(jsondata);
		return jsonArray;
	}

	@Override
	public Object resolveArgument(MethodParameter methodParameter,
			NativeWebRequest webRequest) throws Exception {
		String paramName = methodParameter.getParameterName();
		Map<String, Object> commandMap = new HashMap<String, Object>();
		HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
			
		if (paramName.toUpperCase().indexOf("MAP") != -1) {

			Enumeration<?> enumeration;
			enumeration = request.getParameterNames();

			while (enumeration.hasMoreElements()) {
				String key = (String) enumeration.nextElement();
				String[] values = request.getParameterValues(key);
				if (values != null) {
					commandMap.put(key, (values.length > 1) ? values : StringUtil.unescapeHtml(values[0]));
				}
			}
			
			String models = ((String)commandMap.get("models"));
			List rtnList=new ArrayList();
			if(models != null){
				models = StringUtil.unescapeHtml(models);
				JSONArray obj = JSONArray.fromObject(models);
				List list = JSONArray.toList(obj, HashMap.class);
				commandMap.put("models", obj);
				commandMap.put("jsonList", list);
			}
		
			return commandMap;
		}
		return WebArgumentResolver.UNRESOLVED;
	}
}